<template>
	<view class="loginview">
		<view class="logo-box">
			<view class="logo">
				<image src="/static/logo.png" mode="widthFix"></image>
				<h1 class="h1">视频会议</h1>
			</view>
		</view>
	
		<view class="form-box">
			<u-form :model="form" ref="uForm" :error-type="errorType">
				<u-form-item class="form-item" prop="name"><u-input class="form-input" label-width="0" v-model="form.name" type="text" placeholder="请输入id" /></u-form-item>
				<u-form-item class="form-item" prop="password"><u-input class="form-input" label-width="0" v-model="form.password" type="password" placeholder="请输入密码" /></u-form-item>
			</u-form>
			<view class="form-text">
				<navigator url="/pages/register/register">马上注册</navigator>
				<navigator url="/pages/register/register">忘记密码</navigator>
			</view>
		</view>
	
		<view class="fiexd-btm"><u-button type="primary" @tap="login">登录</u-button></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					name: '12qw233',
					password: '1q2w3e4r'
				},
				rules: {
					name: [
						{
							required: true,
							message: '请输入id',
							trigger: 'change'
						}
					],
					password: [
						{
							required: true,
							min: 7,
							message: '密码不能少于7个字符',
							trigger: 'change'
						}
					]
				},
				errorType: ['toast', 'border-bottom'],
				grant_type: "password",
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		onLoad() {
			if(uni.getStorageSync("myUsername") && uni.getStorageSync("myPassword")) {
				this.form.name = uni.getStorageSync("myUsername")
				this.form.password = uni.getStorageSync("myPassword")
			}
			this.$disp.on("em.xmpp.error.passwordErr", ()=> {
				this.$helper.toast('用户名或密码错误')
			})
		},
		methods: {
			login() {
				this.$helper.toast('Link Start...', 2000, false, 'loading')
				this.$refs.uForm.validate(valid => {
					if (!valid) {
						console.log('验证失败')
						return
					} else {
						uni.setStorage({
							key: 'myUsername',
							data: this.form.name
						})
						uni.setStorage({
							key: 'myPassword',
							data: this.form.password
						})
						uni.setStorage({
							key: 'myGrantType',
							data: this.grant_type
						})
				
						this.$conn.open({
							apiUrl: this.$im.config.apiURL,
							user: this.form.name,
							pwd: this.form.password,
							grant_type: this.grant_type,
							appKey: this.$im.config.appkey
						})
					}
				})
			}
		}
	}
	
</script>

<style lang="scss">
.logo-box {
	padding: 100upx $uni-page-padding 30upx;
	text-align: center;

	.h1 {
		margin-bottom: 30upx;
		font-size: 48upx;
		font-weight: normal;
		color: #09f;
	}

	image {
		margin: 0 auto 20upx;
		display: block;
		width: 140upx;
		height: 140upx;
		border-radius: 140upx;
	}
}

.form-box {
	padding: $uni-page-padding;
	
	.form-item {
		display: flex;
		margin-bottom: 30upx;
		padding: 10upx 20upx;
		flex-wrap: nowrap;
		border: 1px solid #09f;
		
		.u-form-item {
			padding: 0;
			width: 100%;
		}
	}

	.form-text {
		display: flex;
		justify-content: space-between;
		font-size: 24upx;
		color: #09f;
	}
}
</style>
